/**
 * 模块数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.ModuleGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,
	
	initComponent: function(){
		var gridStructure = [{  
			name: 'moduleId',
			hidden: true
		},{
			name: 'moduleCode',  
			header: "模块编号",
			sortable: true,
			width: 50
		},{
			name: 'moduleName',  
			header: "模块名称",
			sortable: true,
			width: 80
		},{
			name: 'moduleTypeLabel',  
			header: "模块类型",
			width: 50
		},{
			name: 'moduleLevel',  
			header: "模块层次",
			width: 50
		},{
			type: 'boolean',
			name: 'isLeaf',  
			header: "是否叶子节点",
			width: 50
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'moduleLevel';
		this.keyField = 'moduleId';
		
		this.deleteMsg = '确认要删除您选择的模块吗？<br/><font color="red">注意</font>：删除模块将删除该模块下的所有子模块和功能。';
		this.deleteMsgBoxWidth = 370;
		
		Ext.ux.grid.ModuleGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(moduleGrid){
				var record = moduleGrid.getSelectionModel().getSelected();
				if(record != null){
					var moduleId = record.get('moduleId');
					this.showModuleInfo(moduleId);
				}
			});
		}
		

		this.on('updateRecord', function(moduleId){
			this.showModuleInfo(moduleId);
		});
		
		this.on('deleteRecord', function(grid, moduleIds){
			this.deleteModule(moduleIds);
		});
	},
	
	//新增顶级模块或子模块，appId不为空且parentModuleId为空表示新增顶级模块，appId为空且parentModuleId不为空表示新增子模块
	addModule: function(appId, parentModuleId, parentModuleName){
		var win = new Ext.ux.org.acapp.ModuleDetailWindow({id:'moduleDetailWindow', title:'新建模块', appId:appId, parentModuleId:parentModuleId, parentModuleName:parentModuleName});
		
		if(appId != null && parentModuleId == null)
			win.showAddTopModuleWin();
		else if(appId == null && parentModuleId != null)
			win.showAddChildModuleWin();
	},
	
	showModuleInfo: function(moduleId){
		var win = new Ext.ux.org.acapp.ModuleDetailWindow({id:'moduleDetailWindow', title:'修改模块', moduleId:moduleId});
		win.showModuleInfo();
	},
	
	deleteModule: function(moduleIds) {
		Ext.Ajax.request({
			url: 'ac-module!delete.action',
			success: function(){
				Ext.getCmp('appTree').refreshApp();
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除机构失败！');
			},
			method: 'POST',
			params:{
				ids: moduleIds
			},
			waitMsg : '正在删除数据，稍候...',
			scope: this
		});
	}
});